<%@ page language="java" pageEncoding="UTF-8"%>
<script type="text/javascript">
	$(function() {
		$('#admin_yhgl_dg').datagrid({
			url : '${pageContext.request.contextPath}/userAction!datagrid.action',
			fit : true,
			fitColumns : true,
			idField : 'id',
			border : false,
			pagination : true,//分页功能
			sortName : 'name',
			sortOrder : 'asc',
			checkOnSelect : false,
			selectOnCheck : false,
			columns : [ [ {
				checkbox : true,
				field : 'id',
				title : '编号',
				width : 150,
			}, {
				field : 'name',
				title : '用户名',
				width : 150,
				sortable : true
			}, {
				field : 'pwd',
				title : '密码',
				width : 150,
				formatter : function(value, row, index) {
					return '<span title="'+value+'">' + value + '</span>';
				}
			}, {
				field : 'createtime',
				title : '注册时间',
				width : 150,
				sortable : true
			}, {
				field : 'modifytime',
				title : '修改时间',
				width : 150,
				sortable : true
			} ] ],
			toolbar : [ {
				text : '添加',
				iconCls : 'icon-add',
				handler : function() {
					$('#admin_yhgl_addDialog input').val('');
					$('#admin_yhgl_addDialog').dialog('open');
				}
			}, '-', {
				text : '删除',
				iconCls : 'icon-remove',
				handler : function() {
					remove();
				}
			}, '-', {
				text : '修改',
				iconCls : 'icon-edit',
				handler : function() {
					var rows = $('#admin_yhgl_dg').datagrid('getChecked');
					if (rows.length == 1) {
						$('#admin_yhgl_editDialog').dialog('open');
						$('#admin_yhgl_editDialog_from input').val('');
						$('#admin_yhgl_editDialog_from').form('load', rows[0]);

					} else {
						$.messager.alert({
							title : '提示',
							msg : '请勾选一条编辑记录！'
						});
					}

				}
			} ]
		});

		$('#admin_yhgl_editDialog').dialog({
			iconCls : 'icon-edit',
			title : '修改用户',
			modal : true,
			closed : true,
			buttons : [ {
				text : '修改',
				iconCls : 'icon-edit',
				handler : function() {
					$('#admin_yhgl_editDialog_from').form('submit', {
						url : '${pageContext.request.contextPath}/userAction!edit.action',
						success : function(data) {
							var obj = $.parseJSON(data);
							if (obj.success) {
								$('#admin_yhgl_dg').datagrid('updateRow', {
									index : $('#admin_yhgl_dg').datagrid('getRowIndex',$('#admin_yhgl_dg').datagrid('getChecked')[0]),
									row : obj.obj
								});
								$('#admin_yhgl_editDialog').dialog('close');
							}
							$.messager.show({
								title : '提示',
								msg : obj.msg
							});
						}
					});
				}
			} ]
		});

	});
	function searchFun() {
		$('#admin_yhgl_dg').datagrid('load', serializeObject($('#admin_yhgl_searchForm'))/* {name : $('#admin_yhgl_layout input[name=name]').val()} */);
	}
	function clearFun() {
		$('#admin_yhgl_layout input[name=name]').val('');
		$('#admin_yhgl_dg').datagrid('load', {});
	}
	function remove() {
		var rows = $('#admin_yhgl_dg').datagrid('getChecked');
		/* var rows = $('#admin_yhgl_dg').datagrid('getSelected');
		var rows = $('#admin_yhgl_dg').datagrid('getSelections'); */
		if (rows.length > 0) {
			$.messager.confirm('警告', '您是否要删除当前选中的项目？', function(r) {
				if (r) {
					var ids = [];
					for (var i = 0; i < rows.length; i++) {
						ids.push(rows[i].id);
					}
					$.ajax({
						url : '${pageContext.request.contextPath}/userAction!remove.action',
						data : {
							ids : ids.join(',')
						},
						dataType : 'json',
						success : function(r) {
							$('#admin_yhgl_dg').datagrid('load');
							$('#admin_yhgl_dg').datagrid('unselectAll');
							$.messager.show({
								title : '提示',
								msg : r.msg
							});
						}
					});
				}
			});
		} else {
			$.messager.show({
				title : '提示',
				msg : '请勾选要删除的记录！'
			});
		}
	}
</script>
<div id="admin_yhgl_layout" class="easyui-layout" data-options="fit:true,border:false">
	<div data-options="region:'north',title:'查询条件',border:false" style="height:55px;">
		<form id="admin_yhgl_searchForm" method="POST">
			查询用户(支持模糊查询)：<input name="name" /> <a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-search',plain:true" onclick="searchFun()">搜索</a> <a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-clear',plain:true" onclick="clearFun()">清空</a>
		</form>
	</div>
	<div data-options="region:'center'">
		<div id="admin_yhgl_dg"></div>
	</div>
</div>

<div id="admin_yhgl_toolbar"></div>
<div id="admin_yhgl_addDialog" class="easyui-dialog" data-options="iconCls:'icon-add',title:'添加用户',modal:true,closed:true,buttons: [{
                    text:'添加',
                    iconCls:'icon-add',
                    handler:function(){
              			$('#admin_yhgl_addDialog_from').form('submit',{
              				url:'${pageContext.request.contextPath}/userAction!add.action',
              				success:function(data){
								var obj = $.parseJSON(data);
								if(obj.success){
									/* $('#admin_yhgl_dg').datagrid('load'); */
									/* $('#admin_yhgl_dg').datagrid('appendRow',obj.obj); */
									$('#admin_yhgl_dg').datagrid('insertRow',{index: 0,row: obj.obj});
									$('#admin_yhgl_addDialog').dialog('close');
								}
								$.messager.show({
									title : '提示',
									msg : obj.msg
								});
              				}
              			});          
                    }
                }]" style="width:300px;height:150px;padding:10px"
	align="center">
	<form id="admin_yhgl_addDialog_from" method="post">
		<table>
			<tr>
				<td>用户名：</td>
				<td><input name="name" class="easyui-validatebox" data-options="required:true" /></td>
			</tr>
			<tr>
				<td>密码：</td>
				<td><input name="pwd" type="password" class="easyui-validatebox" data-options="required:true" />
			</tr>
		</table>


	</form>
</div>

<!-- 修改功能 -->
<div id="admin_yhgl_editDialog" style="width:500px;height:300px;padding:10px" align="center">
	<form id="admin_yhgl_editDialog_from" method="post">
		<table>
			<tr>
				<th>编号</th>
				<td><input name="id" readonly="readonly" /></td>
				<th>登录名称</th>
				<td><input name="name" class="easyui-validatebox" data-options="required:true" /></td>
			</tr>
			<tr>
				<th>密码</th>
				<td><input name="pwd" type="password" readonly="readonly" class="easyui-validatebox" data-options="required:true" /></td>
				<th>创建时间</th>
				<td><input name="createtime" readonly="readonly" /></td>
			</tr>
			<tr>
				<th>最后修改时间</th>
				<td><input name="modifytime" readonly="readonly" /></td>
		</table>


	</form>
</div>
